﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyVoteMVC.Domain;
using System.Data;

namespace MyVoteMVC.Data.Repositories
{
    public class ItemRepository : Repository<VT_Items>
    {
        public override bool Add(VT_Items entity)
        {
            if (entity.ItemImage == null)
            {
                var image = new VT_ItemImage() {  Item_ID = entity.Item_ID};
                ctx.Entry<VT_ItemImage>(image).State = EntityState.Added;
            }

            return base.Add(entity);
        }

        public override bool Delete(VT_Items entity)
        {
            if (entity.ItemImage == null)
            {
                var image = new VT_ItemImage() { Item_ID = entity.Item_ID };

                if (ctx.Entry<VT_ItemImage>(image).State == EntityState.Detached)
                {
                    ctx.ItemImages.Attach(image);
                }
            }

            return base.Delete(entity);
        }
    }
}
